package com.scwatch.core.service.impl;

import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.RowSide;
import com.scwatch.common.orm.SearchFilter;
import com.scwatch.core.domain.LiveBroadcastRoom;
import com.scwatch.core.repository.LiveBroadcastRoomDao;
import com.scwatch.core.service.LiveBroadcastRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class LiveBroadcastRoomServiceImpl implements LiveBroadcastRoomService {


    public Page<LiveBroadcastRoom> findAll(Map<String, String[]> params, Pageable pageable) {
        return dao.findAll(spec(params), pageable);
    }


    public RowSide<LiveBroadcastRoom> findSide(Map<String, String[]> params, LiveBroadcastRoom bean, Integer position, Sort sort) {
        if (position == null) {
            return new RowSide<LiveBroadcastRoom>();
        }
        Limitable limit = RowSide.limitable(position, sort);
        List<LiveBroadcastRoom> list = dao.findAll(spec(params), limit);
        return RowSide.create(list, bean);
    }

    public List<LiveBroadcastRoom> getList() {
        return null;
    }


    public LiveBroadcastRoom get(Integer id) {
        return dao.findOne(id);
    }


    public String replace(String s) {
        return null;
    }

    @Transactional
    public LiveBroadcastRoom save(LiveBroadcastRoom bean) {
        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public LiveBroadcastRoom update(LiveBroadcastRoom bean) {
        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public LiveBroadcastRoom delete(Integer id) {
        LiveBroadcastRoom entity = dao.findOne(id);
        dao.delete(entity);
        return entity;
    }

    @Transactional
    public LiveBroadcastRoom[] delete(Integer[] ids) {
        LiveBroadcastRoom[] beans = new LiveBroadcastRoom[ids.length];
        for (int i = 0; i < ids.length; i++) {
            beans[i] = delete(ids[i]);
        }
        return beans;
    }

    @Override
    public List<LiveBroadcastRoom> getRoomListByParam(Integer liveId) {
        return dao.getRoomListByLiveId(liveId);
    }

    private Specification<LiveBroadcastRoom> spec(Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        Specification<LiveBroadcastRoom> sp = SearchFilter.spec(filters, LiveBroadcastRoom.class);
        return sp;
    }


    private LiveBroadcastRoomDao dao;
    @Autowired
    public void setDao(LiveBroadcastRoomDao dao){this.dao = dao;}
}
